<?php

namespace app\common\logic;

use think\Model;

class OldRecord extends Model
{
    protected $table = 'sys_kq_record';

    /**
     * 根据用户Id返回指定日期的打卡记录
     * @param $user_id
     * @param $time
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getWorkLog($user_id, $time)
    {
        $ret = self::where(['userId' => $user_id, 'DATE(FROM_UNIXTIME(time))' => $time])
            ->field("RIGHT(FROM_UNIXTIME(time),8) time,time time_unix")->distinct(true)->order('time asc')->select();
        return $ret;
    }

    public static function getRecordByUidANDTime($uid, $time)
    {
        $where['userId'] = $uid;
        $where['time'] = ['between', $time];

        $ret = self::where($where)->field("userId,COUNT(userId) counts,GROUP_CONCAT(time ORDER BY time ASC) log,LEFT(FROM_UNIXTIME(time,'%Y-%m-%d %T'),10) time")
            ->group("userId,LEFT(FROM_UNIXTIME(time,'%Y-%m-%d %T'),10)")->order('userId,time desc')->select();
        return $ret;
    }

}